package com.example.sample.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.app.BaseActivity;
import com.example.sample.R;
import com.example.sample.util.SystemUIDisplayer;
import com.example.sample.util.VKUtil;

import net.liteheaven.xblib.common.log.XLog;
import net.liteheaven.xblib.common.ui.DimensionUtil;

import java.lang.reflect.Field;

import static com.example.sample.util.SystemUIDisplayer.MODE_IMMERSIVE;

public class ScreenSizeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //new SystemUIDisplayer(this).setMode(MODE_IMMERSIVE).setStatusBarColor(Color.TRANSPARENT).display();


        setContentView(R.layout.activity_screen_size);

        TextView tv = findViewById(R.id.tv);
        tv.setText("看\n看\n算\n出\n来\n的\n内\n容\n视\n图\n高\n度\n是\n多\n少");
        tv.setBackgroundColor(Color.YELLOW);

        Context ctx = getApplicationContext();

        WindowManager wm = (WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE);

        Display display = wm.getDefaultDisplay();
        Point outSize = new Point();
        display.getSize(outSize);
        int height = outSize.y;
        log("height = " + outSize);

        int realHeight = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealSize(outSize);
            realHeight = outSize.y;
            log("real height = " + outSize);
        }

        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        log("wm dm height = " + dm.heightPixels);

        DisplayMetrics dm2 = ctx.getResources().getDisplayMetrics();
        int resHeight = dm2.heightPixels;
        log("resource height = " + resHeight);

        int statusBarHeight = getStatusBarHeight(this);
        log("status bar height = " + statusBarHeight);

        int navHeight = VKUtil.getNavigationBarHeight(this);
        log("navigation bar height = " + navHeight);

        int dp1 = DimensionUtil.dip2px(this, 1);

        tv.setLayoutParams(new LinearLayout.LayoutParams(-1, realHeight - statusBarHeight /*- navHeight*/ - dp1));
    }

    private void log(String log){
        XLog.d(ScreenSizeActivity.class.getSimpleName(), log);
    }

    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, sbar = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sbar = context.getResources().getDimensionPixelSize(x);
            return sbar;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sbar;
    }
}
